/*
 * MainActionHandler.java
 *
 *  
 */
package org.msb.finance.app;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.msb.finance.data.Account;

/**
 * This {@code MainActionHandler} class handles all of the major actions of the application.
 * 
 * @author Marc Boudreau
 * 
 */
public class MainActionHandler implements ActionListener {

	/*
	 * 
	 */
	private NewAccountDialog newAccountDialog;

	/*
	 * 
	 */
	private AccountDetailsDialog editAccountDetailsDialog;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals("newfile")) {
			doNewFileAction();
		} else if (command.equals("openfile")) {
			doOpenFileAction();
		} else if (command.equals("newaccount")) {
			doNewAccountAction();
		} else if (command.equals("editaccountdetails")) {
			System.out.println(e.paramString());
			System.out.println(e.getSource());
		}
	}

	/*
	 * 
	 */
	void doNewFileAction() {

	}

	/*
	 * 
	 */
	void doOpenFileAction() {

	}

	/*
	 * 
	 */
	void doNewAccountAction() {
		NewAccountDialog dialog = getNewAccountDialog();
		dialog.setVisible(true);

		String name = dialog.getAccountName();
		AccountTypeEnum type = dialog.getAccountType();
		Account account = null;

		try {
			account = type.getAccountClass().newInstance();
		} catch (IllegalAccessException ex) {
			// TODO: Warn the user.
		} catch (InstantiationException ex) {
			// TODO: Warn the user.
		}

		if (null == account) {
			// TODO: Warn the user and then return.

			return;
		}

		account.setName(name);
		FinanceApp.getCabinet().addAccount(account);
	}

	/*
	 * 
	 */
	void doEditAccountDetailsAction(Account account) {
		AccountDetailsDialog dialog = getAccountDetailsDialog();
		dialog.editAccount(account);
	}

	/*
	 * 
	 */
	private NewAccountDialog getNewAccountDialog() {
		if (null == this.newAccountDialog) {
			this.newAccountDialog = new NewAccountDialog(FinanceApp.getFrame());
		}

		return this.newAccountDialog;
	}

	/*
	 * 
	 */
	private AccountDetailsDialog getAccountDetailsDialog() {
		if (null == this.editAccountDetailsDialog) {
			this.editAccountDetailsDialog = AccountDetailsDialog.createDialog(FinanceApp.getFrame(), null);
		}

		return this.editAccountDetailsDialog;
	}

}
